<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            border: 1px solid red;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            line-height: 200px;
            text-align: center;
        }
    </style>
</head>

<body>
    <button>打开弹框</button>
    <button>关闭弹框</button>

    <script>
        let getSingleton = function(fn) {
            var result;
            return function() {
                return result || (result = fn.apply(this, arguments)); // 确定this上下文并传递参数
            }
        }
        let createAlertMessage = function(html) {
            var div = document.createElement('div');
            div.innerHTML = html;
            div.style.display = 'none';
            document.body.appendChild(div);
            return div;
        }

        let createSingleAlertMessage = getSingleton(createAlertMessage);
        var button = document.querySelectorAll('button');
        button[0].addEventListener('click', function() {
            // 多次点击只会产生一个弹窗

            let alertMessage = createSingleAlertMessage('您的知识需要付费充值！');
            alertMessage.style.display = 'block';

        })
        button[1].addEventListener('click', function() {
            // 多次点击只会产生一个弹窗

            let alertMessage = createSingleAlertMessage('您的知识需要付费充值！');
            alertMessage.style.display = 'none';

        })
    </script>
</body>

</html>